<?php
include_once ('class.mssql.php');

class RegisterResponsible{
    private $tag_id_responsible;
    private $name;
    private $lastname1;
    private $lastname2;
    private $rut;
    private $position;
    private $income_date;
    private $company_income_date;
    private $birthdate;
    private $notes;
    private $cod_mainteiner;
    private $comtec_id_mainteiner;
    private $status_reg;
    private $email;
    private $password;

    private $errors;
    private $token;

    public function __construct(){
        $this->errors   = array();
        
        $this->tag_id_responsible = $_POST['tag_id_responsible'];

        $this->name         = $_POST['name'];
        $this->lastname1    = $_POST['lastname1'];
        $this->lastname2    = $_POST['lastname2'];
        $this->rut          = $_POST['rut'].$_POST['rut_cod'];
        $this->position	    = $_POST['position'];
        $this->income_date  = date('Ymd H:i:s');
        //TODO verificar que las fechas lleguen completas
        $this->company_income_date = $_POST['income_year'].$_POST['income_month'].$_POST['income_day']." 00:00:00";
        $this->birthdate    = $_POST['birthday_year'].$_POST['birthday_month'].$_POST['birthday_day']." 00:00:00";
        $this->notes        = $_POST['notes'];
        if(strlen($this->notes) > 400)
        {
         $this->notes = substr($this->notes,0,400);   
        }
        $this->cod_mainteiner = $_POST['cod_mainteiner'];
        $this->comtec_id_mainteiner = $_POST['comtec_id_mainteiner'];
        $this->status_reg   = 1;
        $this->email = $_POST['email'];
        $this->password = $_POST['password'];

        $this->token    = $_POST['token'];
    }

    public function process(){
        if($this->valid_token() && $this->valid_data())
            $this->register();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var){
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var){
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function register(){
        $db = new MSSQL();
        
        $passwmd5 = md5($this->password);
          
        $result = $db->query("
                              INSERT INTO users (
                              comtec_id_mainteiner,                                                           
                              email,
                              passwmd5,
                              access_level,
                              name,
                              lastname1,
                              lastname2,
                              position,
                              status_reg
                              ) VALUES (
                                $this->comtec_id_mainteiner,                             
                                '$this->email',
                                '$passwmd5',
                                4,
                                '$this->name',
                                '$this->lastname1',
                                '$this->lastname2',
                                '$this->position',
                                1
                               )");
          
        $result =  $db->fetch_array($db->query("SELECT comtec_id_user FROM users WHERE email = '$this->email'; "));
        $comtec_id_user = $result['comtec_id_user'];

        $stmt = $db->query("INSERT INTO responsible(
                        comtec_id_mainteiner,
                        company_income_date,
			tag_id_responsible,
			name,
			lastname1,
			lastname2,
			rut,
			position,
			income_date,
			birthdate,
			notes,
			cod_mainteiner,
                        comtec_id_user,
			status_reg
        ) VALUES (
            $this->comtec_id_mainteiner,
            '$this->company_income_date',    
            '$this->tag_id_responsible',
            '$this->name',
            '$this->lastname1',
            '$this->lastname2',
            '$this->rut',
            '$this->position',
            '$this->income_date',
            '$this->birthdate',
            '$this->notes',
            '$this->cod_mainteiner',
             $comtec_id_user,
            $this->status_reg
        )");

        if(sqlsrv_rows_affected($stmt) < 1)
            $this->errors[] = 'No se puede procesar la inscripci&oacute;n';
    }

    public function user_exists(){
        $db = new MSSQL();
        $stmt = $db->query("SELECT comtec_id_responsible FROM responsible WHERE tag_id_responsible = '$this->tag_id_responsible'");

        return $db->num_rows($stmt)? 1 : 0;
    }

    public function show_errors(){
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_data(){
        if($this->user_exists())
            $this->errors[] = 'Encargado ya existe';

        return count($this->errors)? 0 : 1;
    }

    public function valid_token(){
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}

?>
